/*
 * @Author: Administrator
 * @Date:   2017-09-04 15:20:38
 * @Last Modified by:   Administrator
 * @Last Modified time: 2018-01-23 10:29:16
 */
const path = require('path');
const webpack = require('webpack'); 
const ROOT_PATH = path.resolve(__dirname); 
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const config = {
  devtool: '#source-moduls-eval-map',
  entry: {
    // index:['babel-polyfill','./src/index.jsx','webpack-dev-server/client?http://localhost:8087']
    index:['react-hot-loader/patch', './src/index.jsx',]
    // admin:['es6-promise','./src/admin.jsx']
  },
  output: {
    path: path.join(__dirname, 'dist'),
    publicPath: '/dist/',
    filename: 'js/[name].js'
  },
  resolve: {
    extensions: ['.js', '.jsx']
  },
  module: {
    rules:[
      {
        enforce:'pre',
        test:/\.(js|jsx)$/,
        loader:'eslint-loader',
        exclude: '/node_modules/'
      },
      {test:/\.jsx$/,loader:'babel-loader',exclude:'/node_modules/' },
      {test:/\.js$/,loader:'babel-loader',exclude:'/node_modules/' },
      {
        test: /\.(gif|jpg|png|woff|svg|eot|ttf)\??.*$/,
        loader: 'url-loader?limit=20000&name=resource/[name].[ext]'
      },
      {
        test: /\.css$/,
        loader: ExtractTextPlugin.extract({
          use: 'css-loader',
          fallback: 'style-loader'
        })
      }
    ]
  },
  plugins: [
    new ExtractTextPlugin("css/[name].css"),
    //处理html
    new HtmlWebpackPlugin({
      filename: 'index.html',
      template: path.join(__dirname, 'src/index.html'),
      inject: true,
      hash: true,
      chunks: ['index'],
      chunksSortMode: 'dependency',
      minify: {
        removeComments: true,
        collapseWhitespace: false
      }
    }),
    new HtmlWebpackPlugin({
      filename: 'index.ejs',
      template: '!!ejs-compiled-loader!' + path.join(__dirname, 'server/template.ejs'),
      inject: true,
      hash: true,
      chunks: ['index'],
      chunksSortMode: 'dependency',
      minify: {
        removeComments: true,
        collapseWhitespace: false
      }
    }),
    new HtmlWebpackPlugin({
     filename: 'admin.html',
     template: path.join(__dirname, 'src/admin.html'),
     inject: true,
     hash: true,
     chunks: ['admin'],
     chunksSortMode: 'dependency',
     minify: {
       removeComments: true,
       collapseWhitespace: false
     }
    }),
    new webpack.HotModuleReplacementPlugin(),
    // dll插件
    // new webpack.DllReferencePlugin({
    //  manifest: require(path.resolve(ROOT_PATH, './dist/lib', 'manifest.json')),
    //  context: ROOT_PATH,
    // }),
  ],
  devServer:{
    host:'0.0.0.0',
    // contentBase:path.join(__dirname,'dist'),
    port:8087,
    overlay:{
      errors:true
    },
    hot:true,
    inline: true,
    compress:true,
    publicPath:'/dist/',
    historyApiFallback:{
      index: '/dist/index.html'
    },
    proxy: {
      '/news/':'http://localhost:3000',
      '/user/':'http://localhost:3000'
    }
  }
}
// if (!process.env.NODE_ENV) {
//  config.entry.index.push('webpack-dev-server/client?http://localhost:8087')
// }
module.exports = config;
